//
//  ContactViewController.m
//  book
//
//  Created by cocoazxj@163.com on 15/4/7.
//  Copyright (c) 2015年 handcool. All rights reserved.
//

#import "ContactViewController.h"
#import "AddViewController.h"
#import "EditViewController.h"
#import "ContactModel.h"

#define CONTACT_PATH [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"contact.arc"]

@interface ContactViewController ()<UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate, AddViewControllerDelegate, EditViewControllerDelegate>
@property (nonatomic, strong) IBOutlet UITableView *tv_main;
@property (nonatomic, strong) IBOutlet UIBarButtonItem *bbi_add;
@property (nonatomic, strong) NSMutableArray *mArray;
@end

@implementation ContactViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    NSArray *arr = @[@"zhang",@"hello",@"nice"];
//    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"con.plist"];
//    [arr writeToFile:path atomically:YES];
//    NSLog(@"---------%@",path);
    
    UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editButton:)];
    // 从右向左排列
    self.navigationItem.rightBarButtonItems = @[self.bbi_add,editButton];
}

- (void)editButton:(UIBarButtonItem *)item
{
    self.tv_main.editing = !self.tv_main.editing;
}

- (NSMutableArray *)mArray
{
    if(!_mArray)
    {
        _mArray = [NSKeyedUnarchiver unarchiveObjectWithFile:CONTACT_PATH];
        if(!_mArray)
        {
            _mArray = [NSMutableArray array];
        }
    }
    return _mArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.mArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"contact";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//    if(!cell)
//    {
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
//    }
    ContactModel *m = self.mArray[indexPath.row];
    cell.textLabel.text = m.name;
    cell.detailTextLabel.text = m.phoneNum;
    return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle == UITableViewCellEditingStyleDelete)
    {
        [self.mArray removeObjectAtIndex:indexPath.row];
        
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        [NSKeyedArchiver archiveRootObject:self.mArray toFile:CONTACT_PATH];
    }
    else if(editingStyle == UITableViewCellEditingStyleInsert)
    {
        ContactModel *m = [[ContactModel alloc] init];
        m.name = @"hello";
        m.phoneNum = @"186";
        [self.mArray insertObject:m atIndex:indexPath.row + 1];
        NSIndexPath *indexPaths = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:0];
        [tableView insertRowsAtIndexPaths:@[indexPaths] withRowAnimation:UITableViewRowAnimationFade];
        [NSKeyedArchiver archiveRootObject:self.mArray toFile:CONTACT_PATH];
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleInsert;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UIViewController *vc = segue.destinationViewController;
    if([vc isKindOfClass:[AddViewController class]])
    {
        AddViewController *add = (AddViewController *)vc;
        add.delegate = self;
    }
    else if([vc isKindOfClass:[EditViewController class]])
    {
        EditViewController *edit = (EditViewController *)vc;
        edit.navigationController.navigationItem.rightBarButtonItem.title = @"返回";
        NSIndexPath *path = [self.tv_main indexPathForSelectedRow];
        ContactModel *m = self.mArray[path.row];
        edit.contactModel = m;
        edit.delegate = self;
    }
}

- (void)addViewControllerDelegate:(AddViewController *)controller contactModel:(ContactModel *)model
{
    [self.mArray addObject:model];
    [NSKeyedArchiver archiveRootObject:self.mArray toFile:CONTACT_PATH];
    [self.tv_main reloadData];
}

- (void)editViewControllerDelegate:(EditViewController *)controller contactModel:(ContactModel *)model
{
    [NSKeyedArchiver archiveRootObject:self.mArray toFile:CONTACT_PATH];
    [self.tv_main reloadData];
}
- (IBAction)logoutButtonClicked:(UIBarButtonItem *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
@end
